/* SPDX-License-Identifier: GPL-2.0
 *
 * Copyright (C) 2016-2019, LomboTech Co.Ltd.
 * Authors:
 *	lomboswer <lomboswer@lombotech.com>
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 */

#ifndef _LOMBO_CRTC_H_
#define _LOMBO_CRTC_H_

/* color space format */
enum {
	LOMBO_CSFMT_RGB,
	LOMBO_CSFMT_YUV,
	LOMBO_CSFMT_NUM,
};

struct drm_file;
struct drm_pending_vblank_event;
struct lombo_tcon;
struct lombo_dc;

typedef struct lombo_color_property {
	#define CRTC_COLOR_MIN_VALUE	0
	#define CRTC_COLOR_MAX_VALUE	100
	#define CRTC_COLOR_PROP_CNT	12

	struct drm_property *prop[CRTC_COLOR_PROP_CNT];
	int prop_value[CRTC_COLOR_PROP_CNT];
} lombo_color_property_t;

struct lombo_crtc {
	struct drm_crtc crtc;

	struct lombo_tcon *tcon;
	struct lombo_dc *dc;

	lombo_color_property_t color;
	/* Properties for crtc dc */
	struct drm_property *prop_blk_plane_ids;
	struct drm_property_blob *bl_plane_id;

	struct drm_pending_vblank_event *event;

	uint32_t csfmt; /* tcon out csfmt */
	uint8_t is_interlace;
	uint8_t field_pol; /* for interlace, 0 is output(0/2/4...) */

	uint8_t csc_bypass;
};

struct lombo_fb_cfg {
	struct drm_framebuffer *fb;

	/* src_x/y/w/h are Q16 */
	unsigned int src_x;
	unsigned int src_y;
	unsigned int src_w;
	unsigned int src_h;

	int crtc_x;
	int crtc_y;
	unsigned int crtc_w;
	unsigned int crtc_h;

	int color_space; /* color encording */
	int color_range;
	int rotation;
};

static inline struct lombo_crtc *to_lombo_crtc(struct drm_crtc *crtc)
{
	return container_of(crtc, struct lombo_crtc, crtc);
}

struct lombo_crtc *lombo_crtc_init(struct drm_device *drm,
	struct lombo_dc *dc, struct lombo_tcon *tcon);

void lombo_crtc_handle_vblank_events(struct lombo_crtc *ecrtc);

/* for spi panel flush api */
typedef void (*panel_flush_cb_t)(void);
int lombo_crtc_register_panel_flush_cb(panel_flush_cb_t cb);

/* lombo wb ioctl wrapper */
int lombo_wb_set_para_ioctl(struct drm_device *drm,
	void *data, struct drm_file *file);
int lombo_wb_queue_buf_ioctl(struct drm_device *drm,
	void *data, struct drm_file *file);
int lombo_wb_dequeue_buf_ioctl(struct drm_device *drm,
	void *data, struct drm_file *file);
int lombo_wb_start_ioctl(struct drm_device *drm,
	void *data, struct drm_file *file);
int lombo_wb_stop_ioctl(struct drm_device *drm,
	void *data, struct drm_file *file);

/* lombo drm util apis */

int lombo_dev_get_id(struct device *dev, const char *s);

int lombo_fb_kmap(uint64_t phy_addr, uint32_t size, int cache,
	void **map_vaddr, uint32_t *map_offset);
void lombo_fb_kunmap(void **map_vaddr);

int lombo_strtou64(const char *s, char **end, uint64_t *res, uint32_t base);
int lombo_strtou32(const char *s, char **end, uint32_t *res, uint32_t base);
#define lombo_strtou64_base16(s, end, res)  \
	lombo_strtou64(s, end, res, 16)
#define lombo_strtou32_base16(s, end, res)  \
	lombo_strtou32(s, end, res, 16)

#endif /* #ifndef _LOMBO_CRTC_H_ */
